package com.block.datapush.service.apipush.utils;

import cn.hutool.core.util.StrUtil;
import com.block.common.enums.BaseTypeInfoEnum;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * 诺享钱包 双授权
 */
public class JiKeApisUtil {


    //职业
    public static String getjob(String job) {
        if (StrUtil.equals(BaseTypeInfoEnum.Gongzuo.gz001.getValue(), job)) {
            return "2";
        } else if (StrUtil.equals(BaseTypeInfoEnum.Gongzuo.gz002.getValue(), job)) {
            return "4";
        } else if (StrUtil.equals(BaseTypeInfoEnum.Gongzuo.gz003.getValue(), job)) {
            return "1";
        }
        return "3";
    }

    public static Integer getShouRu(String shouru) {
        if (StrUtil.equals(BaseTypeInfoEnum.Shouru.sr001.getValue(), shouru)) {
            return 2000;
        } else if (StrUtil.equals(BaseTypeInfoEnum.Shouru.sr002.getValue(), shouru)) {
            return 5000;
        } else if (StrUtil.equals(BaseTypeInfoEnum.Shouru.sr003.getValue(), shouru)) {
            return 10000;
        }
        return 15000;
    }

    public static String getloan(String loan) {
        if (StrUtil.equals(BaseTypeInfoEnum.LoanAmount.la001.getValue(), loan)) {
            return "1";
        } else if (StrUtil.equals(BaseTypeInfoEnum.LoanAmount.la002.getValue(), loan)) {
            return "2";
        } else if (StrUtil.equals(BaseTypeInfoEnum.LoanAmount.la003.getValue(), loan)) {
            return "3";
        } else if (StrUtil.equals(BaseTypeInfoEnum.LoanAmount.la004.getValue(), loan)) {
            return "4";
        }
        return "5";
    }


    public static String getLoanTime(String loanTime) {
        if (StrUtil.equals(BaseTypeInfoEnum.LoanTime.lt001.getValue(), loanTime)) {
            return "01";
        }
        return "02";
    }



    public static String getXinYongKa(String xinyongka) {
        if (StrUtil.equals(BaseTypeInfoEnum.XinYongKa.xyk001.getValue(), xinyongka)) {
            return "0";
        }
        return "3";
    }

    //社保
    public static String getSocialInsurance(String shebao) {
        if (StrUtil.equals(BaseTypeInfoEnum.Shebao.sb001.getValue(), shebao)) {
            return "0";
        }
        return "2";
    }

    //公积金
    public static String getGjj(String gongjijin) {
        if (StrUtil.equals(BaseTypeInfoEnum.Gjj.gjj001.getValue(), gongjijin)) {
            return "0";
        }
        return "2";
    }

    //房产情况
    public static String getHouse(String house) {
        if (StrUtil.equals(BaseTypeInfoEnum.House.hu001.getValue(), house)) {
            return "0";
        }
        return "1";
    }

    //车产情况
    public static String getCar(String car) {
        if (StrUtil.equals(BaseTypeInfoEnum.Car.car001.getValue(), car)) {
            return "0";
        }
        return "1";
    }

    //投保情况
    public static String getbaodan(String baodan) {
        if (StrUtil.equals(BaseTypeInfoEnum.Baoxian.bx001.getValue(), baodan)) {
            return "0";
        }
        return "1";
    }

    //芝麻分
    public static String getZhimaScore(String zhima) {
        if (StrUtil.equals(BaseTypeInfoEnum.Zhima.zm001.getValue(), zhima)) {
            return "0";
        } else if (StrUtil.equals(BaseTypeInfoEnum.Zhima.zm002.getValue(), zhima) || StrUtil.equals(BaseTypeInfoEnum.Zhima.zm003.getValue(), zhima)) {
            return "1";
        } else if (StrUtil.equals(BaseTypeInfoEnum.Zhima.zm004.getValue(), zhima)) {
            return "2";
        }
        return "3";
    }



    public static Integer getBaiTiao(String baitiao) {
        if (StrUtil.equals(BaseTypeInfoEnum.Baitiao.bt001.getValue(), baitiao)) {
            return 0;
        }
        return 4;
    }


    public static String getXueli(String xueli) {
        if (StrUtil.equals(BaseTypeInfoEnum.XueLi.xl001.getValue(), xueli) || StrUtil.equals(BaseTypeInfoEnum.XueLi.xl002.getValue(), xueli)) {
            return "1";
        }else if(StrUtil.equals(BaseTypeInfoEnum.XueLi.xl003.getValue(), xueli)){
            return "2";
        }
        return "3";
    }

    public static String code = "UTF-8";
    public static String encryptAes(String data, String secretKey) {
        try {
            byte[] encrpyted = tripleAes(Cipher.ENCRYPT_MODE, data.getBytes(code), secretKey.getBytes());
            byte[] encoded = Base64.encodeBase64(encrpyted);
            return new String(encoded);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
    private static byte[] cipherAes(String algorithm, String transformation, int opmode, byte[] data, byte[] iv, byte[] secretKey) {
        try {
            Cipher cipher = Cipher.getInstance(transformation);
            SecretKeySpec keyspec = new SecretKeySpec(secretKey, algorithm);
            IvParameterSpec ivspec = new IvParameterSpec(iv);

            cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
            byte[] encrypted = cipher.doFinal(data);
            return encrypted;
        } catch (InvalidKeyException | NoSuchAlgorithmException
                 | NoSuchPaddingException
                 | IllegalBlockSizeException | BadPaddingException |
                 InvalidAlgorithmParameterException e) {
            throw new RuntimeException(e);
        }
    }
    private static byte[] tripleAes(int opmode, byte[] data, byte[] secretKey) {
        return cipherAes("AES", "AES/CBC/PKCS5Padding", opmode, data, "dxjf18129979469s".getBytes(), secretKey);
    }

}
